package com.guangyuan.network.mvc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.velocity.Template;

import com.guangyuan.network.mvc.handler.AbstractHandler;
import com.guangyuan.network.mvc.handler.Handler;
import com.guangyuan.network.mvc.handler.HandlerMap;
import com.guangyuan.network.mvc.view.ContextAndTemplate;
import com.guangyuan.network.util.Messages;

/**
 * Servlet implementation class SimpleController
 */
public class SimpleController extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(SimpleController.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SimpleController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	private void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		// !FIXME 将这些配置配置到web.xml文件中去
		System.out.println("test");
		response.setCharacterEncoding(Messages.getString("SimpleController.characterEncoding")); //$NON-NLS-1$
		response.setContentType(Messages.getString("SimpleController.contentType")); //$NON-NLS-1$

		PrintWriter pw = response.getWriter();

		String uri = request.getRequestURI();
		String contextPath = request.getContextPath();
		uri = uri.substring(contextPath.length() + 1);

		boolean isError = true;
		Handler handler = HandlerMap.getHandler(uri);
		if (null == handler) {
			//FIXME 如果用户访问页面不存在应该怎样处理，velocity不区分大小写
			//处理单纯是页面的情况
			Template template = AbstractHandler.getTemplate(uri);
			if(null != template) {
				template.merge(null, pw);
			}
			return;
		}

		ContextAndTemplate ct = handler.handler(request, response,
				getServletContext());

		try {
			if (null != ct) {
				ct.getTemplate().merge(ct.getContext(), pw);
				isError = false;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			if (isError) {
				//FIXME 如果用户访问页面不存在应该怎样处理
				return;

			}
			pw.flush();
			pw.close();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		AbstractHandler.initVelocity(config, config.getServletContext());
	}

}
